package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.agreement;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.agreement.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.agreement.AgreementDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.agreement.AgreementConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.agreement.AgreementService;

@Tag(name = "管理后台 - 协议文档")
@RestController
@RequestMapping("/fengdong.smartelderly/agreement")
@Validated
public class AgreementController {

    @Resource
    private AgreementService agreementService;

    @PostMapping("/create")
    @Operation(summary = "创建协议文档")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:create')")
    public CommonResult<Long> createAgreement(@Valid @RequestBody AgreementCreateReqVO createReqVO) {
        return success(agreementService.createAgreement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新协议文档")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:update')")
    public CommonResult<Boolean> updateAgreement(@Valid @RequestBody AgreementUpdateReqVO updateReqVO) {
        agreementService.updateAgreement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除协议文档")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:delete')")
    public CommonResult<Boolean> deleteAgreement(@RequestParam("id") Long id) {
        agreementService.deleteAgreement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得协议文档")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:query')")
    public CommonResult<AgreementRespVO> getAgreement(@RequestParam("id") Long id) {
        AgreementDO agreement = agreementService.getAgreement(id);
        return success(AgreementConvert.INSTANCE.convert(agreement));
    }

    @GetMapping("/list")
    @Operation(summary = "获得协议文档列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:query')")
    public CommonResult<List<AgreementRespVO>> getAgreementList(@RequestParam("ids") Collection<Long> ids) {
        List<AgreementDO> list = agreementService.getAgreementList(ids);
        return success(AgreementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得协议文档分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:query')")
    public CommonResult<PageResult<AgreementRespVO>> getAgreementPage(@Valid AgreementPageReqVO pageVO) {
        PageResult<AgreementDO> pageResult = agreementService.getAgreementPage(pageVO);
        return success(AgreementConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出协议文档 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:agreement:export')")
    @OperateLog(type = EXPORT)
    public void exportAgreementExcel(@Valid AgreementExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgreementDO> list = agreementService.getAgreementList(exportReqVO);
        // 导出 Excel
        List<AgreementExcelVO> datas = AgreementConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "协议文档.xls", "数据", AgreementExcelVO.class, datas);
    }

}
